import { loginAsync } from '@/api/login'

export default {
  namespaced: true,
  state: {
    userInfo: null,
    token: '',
  },
  mutations: {
    loginSuccess(state, payload) {
      state.userInfo = payload.userInfo
      state.token = payload.token
    },
    resetUser(state) {
      state.userInfo = null
      state.token = ''
    },
  },
  actions: {
    // 用户登录处理的 action
    async loginAction(context, payload) {
      // 调用异步方法，请求登录接口
      const result = await loginAsync(payload.phone, payload.code)
      // 登录成功，则修改状态数据
      if (result.userInfo) {
        context.commit('loginSuccess', result)
        return true
      } else {
        context.commit('resetUser')
        return false
      }
    },
  },
}
